<template>
  <el-dialog title="为角色分配权限" :visible="isShow" @close="closeEvent">
    <!-- 获取已选择项的id
        1:el-tree
            ref="tree"
            node-key='id'
        2:获取已选中项:this.$refs.tree.getCheckedKeys()
        3:设置选中项:this.$refs.tree.setCheckedKeys([‘需要选中项id’])
     -->
    <el-tree
      ref="tree"
      check-strictly
      :data="list"
      :props="{ label: 'name' }"
      show-checkbox
      node-key="id"
      :default-checked-keys="defaultKeys"
    />
    <template #footer>
      <div style="text-align: center">
        <el-button type="primary" @click="submit">确定</el-button>
        <el-button @click="closeEvent">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script>
import { sysPermission } from '@/api/permission'
import { changeData } from '@/utils/index'
import { sysRoleId, sysRoleAssignPrem } from '@/api/setting'
export default {
  props: {
    isShow: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      list: [],
      defaultKeys: [], // 默认选中项的值
      id: ''
    }
  },
  methods: {
    // 关闭事件
    closeEvent() {
      this.$emit('update:isShow', false)
      // 清空已勾选数据
      this.$refs.tree.setCheckedKeys([])
    },
    // 获取权限列表数据
    async getData() {
      if (this.list.length === 0) {
        const res = await sysPermission()
        this.list = changeData(res.data, '0')
        console.log('权限列表', res)
      }
    },
    // 获取角色详情
    async getInfo(id) {
      this.id = id
      const res = await sysRoleId(id)
      this.defaultKeys = res.data.permIds
      console.log('角色详情', res)
    },
    // 确定点击调用
    async submit() {
      // console.log(this.$refs.tree.getCheckedKeys())
      await sysRoleAssignPrem({
        id: this.id,
        permIds: this.$refs.tree.getCheckedKeys()
      })
      // 提示
      this.$message.success('分配权限成功')
      // 关闭弹框
      this.closeEvent()
    }
  }
}
</script>
<style></style>
